
#include "hardware.h"
#include "sys.h"
#include "macros.h"
#include "gpio.h"
#include "atd.h"
#include "status.h"


#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "fout.h"
#include "cmd.h"
#include "modem.h"
#include "debug.h"



void blink ( void *pvParameters );
void blink ( void *pvParameters )
{
	unsigned int i;

	LED1_DIR = PIN_DIR_OUTPUT;

	for(;;)
	{

		LED1 ^= 1;
		for(i=0; i < 500000; i++ )
		{
			NOP();
		}
	}
}

void task1 ( void *pvParameters );
void task1 ( void *pvParameters )
{
	unsigned int i,c=0;
		
	LED1_DIR = PIN_DIR_OUTPUT;

	for(;;)
	{

		LED1 ^= 1;

//		for(i=0; i < 1000000; i++ ) NOP();

		vTaskDelay(500 MSEC);
	  
		//fout(0,"=== 11111111111111111111111111111 === %s; counter c:%d\n\r",__FUNCTION__,c++);
	}
}


void task2 ( void *pvParameters );
void task2 ( void *pvParameters )
{
	unsigned int i,c = 0;
		
	LED2_DIR = PIN_DIR_OUTPUT;
	LED3_DIR = PIN_DIR_OUTPUT;
	LED4_DIR = PIN_DIR_OUTPUT;

	LED2 = 0;
	LED3 = 0;
	LED4 = 0;

	for(;;)
	{

		LED2 ^= 1;
		LED3 ^= 1;
		LED4 ^= 1;

		/* for(i=0; i < 1000000; i++ ) */
		/* { */
		/* 	NOP(); */
		/* } */

		vTaskDelay(2000 MSEC);
		
		//dbg("=== 22222222222 === counter c:%d\n\r",c++);
		
		//xTaskCreate(blink,"b",100,NULL,(tskIDLE_PRIORITY +1),NULL);
	}
}


void task_msg ( void *pvParameters );

typedef void (*interrupt_function)( void );// __attribute__ ((naked));
interrupt_function intfun;

//extern void vPreemptiveTick( void ) __attribute__((naked));

void irq_handler( void ) __attribute__ ((naked));
//void irq_handler( void ) __attribute__ ((interrupt("IRQ")));
//void irq_handler( void ) __attribute__ ((naked,noinline));
//void irq_handler( void ) __attribute__ ((section("privileged_data")));
//void irq_handler( void ) __attribute__ ((section("privileged_functions")));
/* void irq_handler( void ) */
/* { */
/* 	//DBG(""); */
/* 	   /\* __asm volatile *\/ */
/* 		/\* 	( *\/ */
/*       /\*       " ldr         r14, =0xFFFFF000  \t\n" \ *\/ */
/*       /\*       " ldr         r0 , [r14, #0xF00]  \t\n" \ *\/ */
/* 		/\* 		" mov         r14, pc \t\n" \ *\/ */
/*       /\*       " bx          r0 \t\n" *\/ */

/* 		/\* 		); *\/ */

/* 	portSAVE_CONTEXT(); */

	
/* 	intfun = (interrupt_function)VICVectAddr; */
/* //	DBG("intfun: %d",(unsigned int)intfun); */

/* 	if ( intfun != NULL ) */
/* 	{ */
		
/* 		(*intfun)(); */
	
/* 	}   */

/*  	VICVectAddr = 0; // Clear interrupt in VIC. */
	
/* 	portRESTORE_CONTEXT(); */

/* } */

/* void vPortYieldProcessor ( void ) */
/* { */
/* //	DBG(""); //Dima */
/* } */

void irq_handler ( void )
{
//	DBG(""); //Dima
}

void wdt_task ( void *pvParameters )
{
	unsigned int i;

	wdt_init(4);

	for(;;)
	{
		vTaskDelay(500 MSEC);
		wdt_reload();
	}
} 

#define SWI_RAM_ADDR			 0x40000008  
#define IRQ_RAM_ADDR        0x40000018
#define FIQ_RAM_ADDR        0x4000001C

#define SWI_RAM_FUNC_ADDR   0x40000028
#define IRQ_RAM_FUNC_ADDR   0x40000038
#define FIQ_RAM_FUNC_ADDR   0x4000003C

extern void vPortYieldProcessor( void );

void vec_remap ( void )
{
	unsigned int * ptr;
	
	ptr = (unsigned int *)SWI_RAM_ADDR;
	*ptr = 0xE59FF018;                       //-- ldr pc, [pc, #24]
	
	ptr = (unsigned int *)SWI_RAM_FUNC_ADDR;
	*ptr = &vPortYieldProcessor;
}

//extern void vcom_task ( void *);

int main ( void )
{	
	unsigned char res = 0;
	
	interrupts_disable();
	
	sysInit();
	vec_remap();
	uarts_init();
	
	fout_init();
	cmd_init();
	vcom_init();
	modem_init();
	status_init();

	res =  xTaskCreate(wdt_task,"wdt_task",100,NULL,(tskIDLE_PRIORITY +1),NULL);
	res += xTaskCreate(task1,"task1",100,NULL,(tskIDLE_PRIORITY +1),NULL);
//	res += xTaskCreate(task2,"task2",100,NULL,(tskIDLE_PRIORITY +1),NULL);
	res += xTaskCreate(fout_task,"fout_task",200,NULL,(tskIDLE_PRIORITY +1),NULL);
	res += xTaskCreate(atd_task,"atd_task",200,NULL,(tskIDLE_PRIORITY +1),NULL);
	res += xTaskCreate(cmd_task,"cmd_task",200,NULL,(tskIDLE_PRIORITY +1),NULL);
	res += xTaskCreate(modem_task,"modem",200,NULL,(tskIDLE_PRIORITY +2),NULL);
	res += xTaskCreate(modem_net_task,"modem_net",200,NULL,(tskIDLE_PRIORITY +1),NULL);
	res += xTaskCreate(gpio_task,"gpio_task",200,NULL,(tskIDLE_PRIORITY +1),NULL);
	res += xTaskCreate(status_task,"status",200,NULL,(tskIDLE_PRIORITY +1),NULL);
	
	DBG("\r\n"); //Dima
	DBG(" =============== Cloud Alert ==============="); //Dima
	DBG(" ========== CCLK: %d ==========",CCLK); //Dima
	DBG(" ==========================================="); //Dima
	DBG(" xTaskCreate: %d\r\n",res); //Dima
	
	vTaskStartScheduler();

	
	blink(NULL);
	
	return 0;
}




